#!/usr/bin/python3
import sys

def possible(game):
    return not (game['red'] > 12 or game['green'] > 13 or game['blue'] > 14)

# very unclean, but works
def part1():
    print('part 1')
    game_id = 1
    summed = 0
    for line in sys.stdin:
        left, right = line.strip().split(':')
        game_id = int(left.lstrip('Game '))
        configs = [[subset.strip() for subset in subsets.split(',')] for subsets in right.split(';')]
        games = []
        for config in configs:
            game = {'red': 0, 'green': 0, 'blue': 0}
            reds = greens = blues = 0
            for cubes in config:
                if cubes.endswith(' red'):
                    game['red'] += int(cubes.rstrip(' red'))
                elif cubes.endswith(' green'):
                    game['green'] += int(cubes.rstrip(' green'))
                elif cubes.endswith(' blue'):
                    game['blue'] += int(cubes.rstrip(' blue'))
            games.append(game)

        is_possible = True
        for game in games:
            if not possible(game):
                is_possible = False
                break

        if is_possible:
            summed += game_id

    print(summed)

def part2():
    print('part 2')
    game_id = 1
    powers = 0
    for line in sys.stdin:
        left, right = line.strip().split(':')
        game_id = int(left.lstrip('Game '))
        configs = [[subset.strip() for subset in subsets.split(',')] for subsets in right.split(';')]
        games = []
        mins = {'red': 0, 'green': 0, 'blue': 0}
        for config in configs:
            game = {'red': 0, 'green': 0, 'blue': 0}
            reds = greens = blues = 0
            for cubes in config:
                if cubes.endswith(' red'):
                    game['red'] += int(cubes.rstrip(' red'))
                elif cubes.endswith(' green'):
                    game['green'] += int(cubes.rstrip(' green'))
                elif cubes.endswith(' blue'):
                    game['blue'] += int(cubes.rstrip(' blue'))
                mins['red'] = max(mins['red'], game['red'])
                mins['blue'] = max(mins['blue'], game['blue'])
                mins['green'] = max(mins['green'], game['green'])
            games.append(game)
        powers += mins['red'] * mins['green'] * mins['blue']
    print(powers)

if sys.argv[1] in '1':
    part1()
else:
    part2()
